/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"fmt"

	"strconv"

	devices "gitee.com/zinface/gpio-ctl/pkg/gpio"
	"github.com/spf13/cobra"
)

// getvalueCmd represents the getvalue command
var getvalueCmd = &cobra.Command{
	Use:   "getvalue",
	Short: _desc_getvalue,
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) > 0 {
			// 获取第一个参数
			id, err := strconv.Atoi(args[0])
			if err != nil {
				panic(err)
			}

			// 尝试读取已导出设备的值
			gpio := devices.NewGPIODevice(id)
			if gpio.IsExported() {
				level, _ := gpio.Value()
				fmt.Println(level)
			}
		}
	},
}

func init() {
	rootCmd.AddCommand(getvalueCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// getvalueCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// getvalueCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
